﻿using BW.ME.Exam.Domain.Entities;
using BW.ME.Exam.ErrorCode;
using BW.ME.Exam.Infrastructure.Interfaces;
using BW.ME.Exam.Read.Api.Applications.Commands.FormQueryCommand;
using BW.ME.Exam.Read.Api.Dtos;
using MediatR;

namespace BW.ME.Exam.Read.Api.Applications.CommandHandlers.FormQueryCommandHandler
{
    /// <summary>
    ///我的待办查询命令处理
    /// </summary>
    public class MyPendingQueryCommandHandler : IRequestHandler<MyPendingQueryCommand, ApiResult<ApiPaging<InstanceDTO>>>
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="InstanceRepository">实例仓储</param>
        /// <param name="GClassRepository">班级仓储</param>
        public MyPendingQueryCommandHandler(IBaseRepository<WF_Instance> InstanceRepository, IBaseRepository<GClass> GClassRepository)
        {
            this.InstanceRepository = InstanceRepository;
            this.GClassRepository = GClassRepository;
        }

        public IBaseRepository<WF_Instance> InstanceRepository { get; }
        public IBaseRepository<GClass> GClassRepository { get; }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<ApiResult<ApiPaging<InstanceDTO>>> Handle(MyPendingQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<InstanceDTO>> result=new ApiResult<ApiPaging<InstanceDTO>>();
            result.Code = ResultCode.查询成功;
            result.Message=ResultCode.查询成功.ToString();

            var list = from a in InstanceRepository.GetAll()
                       join b in GClassRepository.GetAll() on a.AppId equals b.ClassId
                       where a.NextOperatorId == request.CurrentUserId && a.FlowStatus != (int)FlowStatus.完成
                      select new InstanceDTO
                    {
                        InstanceId = a.InstanceId,
                        AppId = a.AppId,
                        FlowStatus = a.FlowStatus,
                        FlowStatusName = a.FlowStatusName,
                        StaterId = a.StaterId,
                        StarterName = a.StarterName,
                        CurrentNodeNo = a.CurrentNodeNo,
                        CurrentNodeName = a.CurrentNodeName,
                        CurrentOperatorId = a.CurrentOperatorId,
                        CurrentOperatorName = a.CurrentOperatorName,
                        NextNodeNo = a.NextNodeNo,
                        NextNodeName = a.NextNodeName,
                        NextOperatorId = a.NextOperatorId,
                        NextOperatorName = a.NextOperatorName,
                        CreateTime = a.CreateTime,
                        ClassRoom = b.ClassRoom,
                       
                        ClassTeacher = b.ClassTeacher,
                    };
            var totalCount = list.Count();
            var totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.Size);
            list = list.OrderBy(x => x.InstanceId).Skip((request.Index - 1) * request.Size).Take(request.Size);
            ApiPaging<InstanceDTO> apiPaging = new ApiPaging<InstanceDTO>();
            apiPaging.TotalCount = totalCount;
            apiPaging.TotalPage = totalPage;
            apiPaging.Data = list.ToList();
            result.Data = apiPaging;

            return Task.FromResult(result);
        }
    }
}
